<?php

namespace App\Console\Commands;

use App\Libs\Umeng;
use App\Models\Message;
use App\Models\OrderList;
use App\Models\Subscribe;
use App\Models\User;
use Illuminate\Console\Command;

class HandleSubscribe extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'handle:subscribe';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'handle subscribe';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $time = time();

        $sub_model = new Subscribe;

        //查询当前时间已完成的预约课时

        while(true){

            $sub_list = $sub_model->leftJoin('yy_put_periods as put','put.id','=','yy_subscribes.putime_id')
                                        ->where('yy_subscribes.status',0)//状态未完成
                                        ->where('put.endtime', '<=', $time)//结束时间小于当前时间
                                        ->select('put.endtime','put.startime','put.km','yy_subscribes.*')
                                        ->orderBy('put.startime','desc')
                                        ->limit(500) //每次取500条执行
                                        ->get();

            if(count($sub_list) > 0){

                foreach($sub_list as $sub){
                    //更新学时预约信息状态
                    $sub_model->where('id', $sub->id)->update(['status' => 4]);

                    if($sub->km == 2){
                        $km = '科二实操练习';
                    }else{
                        $km = '科三实操练习';
                    }
                    $msg['content'] = '你完成了'.$sub->coach->name.'教练：'.date('Y-m-d H:i', $sub->startime).' ~ '.date('H:i', $sub->endtime).' 的'.$km;
                    $msg['from_type'] = 3;//消息来源学员
                    $msg['from_id'] = $sub->student_id;
                    $msg['to_type'] = 1;//消息接受学员
                    $msg['to_id'] = $sub->student_id;
                    $msg['created_at'] = date('Y-m-d H:i:s', $time);
                    Message::insert($msg);

                    $student_info = $sub->student;
                    $user_info = User::where('username', $student_info->idcard)->first();

                    //生成未支付订单
                    $orderlist_model = new OrderList;

                    $orderlist_model->userid = $user_info->id;
                    $orderlist_model->username = $student_info->name;
                    $orderlist_model->ordid = date('YmdHis').rand(1000000,9999999);
                    $orderlist_model->ordtime = time();
                    $orderlist_model->ordtitle = '预约学车支付';
                    $orderlist_model->ordbuynum = 1;
                    $orderlist_model->ordprice = $sub->money;
                    $orderlist_model->ordfee = $sub->money;
                    $orderlist_model->payment_buyer_email = $user_info->email;
                    $orderlist_model->productid = $sub->id;
                    $orderlist_model->school_id = $sub->school_id;
                    $orderlist_model->type = 2;
                    $orderlist_model->km = $sub->km;
                    $orderlist_model->coach_id = $sub->coach_id;

                    //先学后付生成未支付订单信息
                    if($sub->paymodel == 1){
                        $orderlist_model->save();
                    }

                    $device_token = $sub->student->device_token;
                    $device_type = $sub->student->device_type;
                    if( $device_token ){
                        $umen = new Umeng($device_type, 3);
                        $umen->sendNotificationToDevices(['title' => '预约提示', 'text' => $msg['content'], 'device_tokens' => $device_token]);
                    }
                }
            }else{
                sleep(3);
            }
        }
    }
}
